package com.example.logintest01;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatSpinner;

import android.accounts.Account;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;

public class EditProfileActivity extends AppCompatActivity {

    private String TAG = "tag";
    private EditText etNickName, etAccount, etSchool,etSign;
    private TextView tvBirthDayTime;
    private RadioButton rbBoy, rbGirl;
    private AppCompatSpinner spinnerCity;

    private String[] cities;
    private int selectedCityPosition;
    private String selectedCity;

    private String birthDay;
    private String birthDayTime;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_profile);


        initView();
        initData();

        initEvent();

    }

    private void initView() {

        etNickName = findViewById(R.id.et_nick_name);
        etAccount = findViewById(R.id.et_account_text);
        etSchool = findViewById(R.id.et_school_text);
        etSign = findViewById(R.id.et_nick_name);


        tvBirthDayTime = findViewById(R.id.tv_birth_time_text);

        rbBoy = findViewById(R.id.rb_boy);
        rbGirl = findViewById(R.id.rb_girl);
        spinnerCity = findViewById(R.id.sp_city);
    }

    private void initData() {
        cities = getResources().getStringArray(R.array.cities);
        getDataFromSpf();
    }

    private void getDataFromSpf() {
        SharedPreferences spfRecord = getSharedPreferences("spfRecord", MODE_PRIVATE);
        String account = spfRecord.getString("account", "");
        String nickName = spfRecord.getString("nick_name", "");
        String city = spfRecord.getString("city", "");
        String gender = spfRecord.getString("gender", "");
        String school = spfRecord.getString("school", "");
        String birthDayTime = spfRecord.getString("birth_day_time", "");
        String sign = spfRecord.getString("sign", "");

        etAccount.setText(account);
        etNickName.setText(nickName);
        etSchool.setText(school);
        etSign.setText(sign);
        tvBirthDayTime.setText(birthDayTime);

        if(TextUtils.equals("男", gender)){
            rbBoy.setChecked(true);
        }

        if(TextUtils.equals("女", gender)){
            rbGirl.setChecked(true);
        }

        for (int i = 0; i < cities.length; i++) {
            if(TextUtils.equals(cities[i], city)){
                selectedCityPosition = i;
                break;
            }
        }

        spinnerCity.setSelection(selectedCityPosition);



    }



    private void initEvent() {

        spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedCityPosition = position;
                selectedCity = cities[position];

                Log.d(TAG, "onItemSelected:--------position--------------" + position);
                Log.d(TAG, "onItemSelected:--------selectedCity--------------" + selectedCity);


            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        tvBirthDayTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatePickerDialog(EditProfileActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
                        int realMonth = month + 1;
                        birthDay = year +"年" + realMonth + "月" + dayOfMonth + "日";

                        popTimePick();
                    }


                }, 2000, 10, 23).show();
            }
        });

    }

    private void popTimePick() {
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
                birthDayTime = birthDay + hourOfDay + "时" + minute + "分钟";
                tvBirthDayTime.setText(birthDayTime);
            }
        }, 12, 36, true).show();
    }

    public void save(View view) {

        String account = etAccount.getText().toString();
        String sign = etSign.getText().toString();
        String school = etSchool.getText().toString();
        String nickName = etNickName.getText().toString();

        String gender = "男";
        if(rbBoy.isChecked()){
            gender = "男";
        }
        if(rbGirl.isChecked()){
            gender = "女";
        }
        SharedPreferences spfRecord = getSharedPreferences("spfRecord", MODE_PRIVATE);
        SharedPreferences.Editor edit = spfRecord.edit();
        edit.putString("account", account);
        edit.putString("sign", sign);
        edit.putString("school", school);
        edit.putString("nickName", nickName);
        edit.putString("birth_day_time", birthDayTime);
        edit.putString("city", selectedCity);
        edit.putString("gender", gender);
        edit.apply();

        this.finish();
    }
}